package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Employee;
import com.itheima.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/*
* 员工管理controller
* */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest req, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername()).eq(Employee::getPassword, password);
        Employee loginEmployee = employeeService.getOne(wrapper);
        if (loginEmployee == null) {
            return R.error("登录失败");
        }

        if (loginEmployee.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        //存取登录人员ID
        req.getSession().setAttribute("employee", loginEmployee.getId());


        return R.success(loginEmployee);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {

        req.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest req, @RequestBody Employee employee) {

        //密码默认123456
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
/*
        //创建时间，修改时间系统时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获取登录人员Id
        Long empId = (Long) req.getSession().getAttribute("employee");
        //创建新用户的Id
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
*/
        employeeService.save(employee);
        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        //StringUtils.isNotEmpty(name)等同于 name != null && name.length > 0

        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
/*        default <E extends IPage<T>> E page(E page) {
            return this.page(page, Wrappers.emptyWrapper());
        }*/
        //返回值就是pageInfo
        employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest req, @RequestBody Employee employee) {
        log.info(employee.toString());
        Long empId = (Long) req.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> employeeById(@PathVariable Long id) {
        log.info("根据id查询员工信息,id :" + id.toString());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId, id);
        Employee updateEmp = employeeService.getOne(wrapper);
        if (updateEmp != null) {
            return R.success(updateEmp);
        }
        return R.error("没有查询到用户信息");
    }
}
